<?php
class Request{
	// 目标手机号码
	public static $mobile = '';
	
	/**
	 * 复合请求
	 */
	public static function complexRequest($data) {
		foreach($data['list'] as $item) {
			$item['referer'] = $data['referer'];
			$item['cookie'] = $data['cookie'];
			
			$log = isset($item['log']) ? $item['log'] : true;
			self::normalRequest($item, $log);
		}
	}
	
	/**
	 * 匹配模式的 复合请求
	 * 匹配表单中的 值, 作为参数传递
	 */
	public static function matchComplexRequest($data) {
		$contents = self::sendRequest(
			$data['match']['url'],
			@ $data['match']['data'],
			array(
				'referer'=> $data['referer'],
				'cookie'=> $data['cookie'],
				'ssl_verifypeer'=> isset($data['match']['ssl_verifypeer']) ? $data['match']['ssl_verifypeer'] : true,
				'is_log'=> false
			)
		);

		$attributes = array();
		foreach($data['match']['attributes'] as $attr => $regExp) {
			preg_match_all($regExp, $contents, $matchs);
			if(!empty($matchs)) {
				$attributes[$attr] = @ $matchs[1][0];
			}
		}
		
		foreach($data['list'] as &$item) {
			$item['url'] = self::replace($item['url'], $attributes);
			$item['data'] = self::replace($item['data'], $attributes);
		}
		
		self::complexRequest($data);
	}
	
	/**
	 * 普通请求
	 */
	public static function normalRequest($data, $isLog = true) {
		$requestData = self::replace($data['data']);
		$url = self::replace($data['url']);
		
		return self::sendRequest(
			$url,
			$requestData,
			array(
				'is_post'=> @ $data['post'],
				'referer'=> @ $data['referer'],
				'cookie'=> @ $data['cookie'],
				'ssl_verifypeer'=> isset($data['ssl_verifypeer']) ? $data['ssl_verifypeer'] : true,
				'is_log'=> $isLog
			)
		);
	}
	
	/**
	 * 发送请求
	 */
	private static function sendRequest($url, $data, $params = array()) {
		$params = array_merge(array(
			'is_post'=>false,
			'referer'=>'',
			'cookie'=>'',
			'is_log'=> true,
			'ssl_verifypeer'=>true,
			'timeout'=>10
		), $params);
		
		$params['is_post'] ? $params['data'] = $data : $url .= '?'.$data;
		
		// 从URL 自动匹配 referer
		if(empty($params['referer'])) {
			$urlData = parse_url($url);
			$params['referer'] = $urlData['scheme'].$urlData['host'];
		}
		
		// 自动匹配 https
		$params['https'] = preg_match("#^https#", $url);
		
		$curl = Curl::model();
		
		// 设置 cookie
		if(!empty($params['cookie'])) {
			$curl->cookie = $params['cookie'];
			$params['use_cookie'] = true;
		}
		$result = $curl->request($url, $params);
		
		if(!$params['is_log']) return $result;
		
		$resultArr = @ json_decode($result);
		$resultArr && $result = $resultArr;
		
		$log = sprintf("\n [time: %s] \n url: %s \n data: %s \n result: %s\n ----end---- \n",
			date('Y-m-d H:i:s'), $url, var_export($data, true), var_export($result, true));
		Helper::log($log);
	}
	
	/**
	 * 变量替换
	 * @param String $string 需要替换的字符串
	 * @param Array $attributes key=>value, 字符串中的 {$key}替换成 value
	 * @return String
	 */
	private static function replace($string, $attributes = array()) {
		$keys = array_keys($attributes);
		$values = array_values($attributes);
		
		$keys = array_map(function($item) {
			return '{$'.$item.'}';
		}, $keys);
		
		return str_replace(
			array_merge(array('{$rand}', '{$mobile}', '{$millisecond}', '{$time}'), $keys),
			array_merge(array(Helper::randNum(), self::$mobile, Helper::millisecond(), time()), $values),
			$string
		);
	}
}